package com.laboratory.servicecustomer.setting.controller;

import com.Laboratory.setting.service._noticeService;
import com.Laboratory.workBench.pojo.notice;
import com.Laboratory.workBench.service.noticeService;
import com.laboratory.servicecustomer.constant.constantArgs;
import com.laboratory.servicecustomer.utilPojo.comm;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author acok
 * @description:TODO
 * @Date 2022/12/30 15:43
 * @Version 1.0
 */
@Controller
@RequestMapping("/_notice")
public class _noticeController {
    @DubboReference(interfaceClass = _noticeService.class,version = "1.0.0")
    private _noticeService _noticeService;
    @DubboReference(interfaceClass = com.Laboratory.workBench.service.noticeService.class,version = "1.0.0")
    private noticeService noticeService;


    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:noticeManage","Root:highest"},logical = Logical.OR)
    @GetMapping("/getNoticeList")
    @ResponseBody
    public Object getAllNoticesToHome(){
        comm comm=new comm();
        try{
            List<notice> notices = noticeService.getAllNotices();
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(notices);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:noticeManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/insertNotice")
    @ResponseBody
    public Object createNotice(@RequestBody notice notice){
        comm comm=new comm();
        try{
            if(_noticeService.createNotice(notice)>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:noticeManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/updateNoticeById")
    @ResponseBody
    public Object updateNoticeById(@RequestBody notice notice){
        comm comm=new comm();
        try{
            if(_noticeService.updateNoticeById(notice)>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:noticeManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/deleteNoticeByIds")
    @ResponseBody
    public Object deleteNoticeByIds(String []ids){
        comm comm=new comm();
        try {
            if(_noticeService.deleteNoticesByIds(ids)>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:noticeManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/getNoticesByCondition")
    @ResponseBody
    public Object getNoticesByCondition(@RequestParam(name = "createDate",required = false) String createDate,
                                        @RequestParam(name = "title",required = false) String title){
        comm comm=new comm();
        try {
            List<notice> notices = _noticeService.getNoticesByCondition(createDate, title);
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(notices);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }
}
